/*
 * Project: Set Sample
 * Copyright (C) 2013 alf.labs gmail com,
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package com.alflabs.setsample.stats;

import android.app.Activity;
import android.content.Context;
import android.util.Log;
import com.google.analytics.tracking.android.EasyTracker;
import com.google.analytics.tracking.android.MapBuilder;

import java.util.Locale;

/**
 * Google Analytics helper rewritten around GA v3
 */
public final class GAHelper {

    public static final String TAG = GAHelper.class.getSimpleName();
    public static boolean sEnabled_NotForTesting = true;

    private GAHelper() {
    }

    public static void activityStart(Activity activity) {
        if (sEnabled_NotForTesting) {
            EasyTracker et = EasyTracker.getInstance(activity);
            if (et == null) { Log.w(TAG, "[start] GA Tracker null"); return; }
            et.activityStart(activity);
        }
    }

    public static void activityStop(Activity activity) {
        if (sEnabled_NotForTesting) {
            EasyTracker et = EasyTracker.getInstance(activity);
            if (et == null) { Log.w(TAG, "[stop] GA Tracker null"); return; }
            et.activityStop(activity);
        }
    }

    /**
     * Sends an event with no data and with the given label.
     *
     * @param event One of the {@link com.alflabs.setsample.stats.GAEvent} names. Category is inferred from event name.
     */
    public static void event(Context context, GAEvent event) {
        String key = event.toString().toLowerCase(Locale.US);
        event(context, key, null, 0);
    }

    /**
     * Sends an event with no data and with the given label.
     *
     * @param event One of the {@link com.alflabs.setsample.stats.GAEvent} names. Category is inferred from event name.
     * @param label An optional label. Can be null.
     */
    public static void event(Context context, GAEvent event, String label) {
        String key = event.toString().toLowerCase(Locale.US);
        event(context, key, label, 0);
    }

    /**
     * Sends an event with no data and with the given label.
     *
     * @param key Key name must be in the form 'category_action'.
     * @param label An optional label. Can be null.
     */
    public static void event(Context context, String key, String label) {
        event(context, key, label, 0);
    }

    /**
     * Sends an event with the given label and given integer data.
     *
     * @param event One of the {@link com.alflabs.setsample.stats.GAEvent} names. Category is inferred from event name.
     * @param label An optional label. Can be null.
     * @param count The integer data associated with the event.
     */
    public static void event(Context context, GAEvent event, String label, long count) {
        String key = event.toString().toLowerCase(Locale.US);
        event(context, key, label, count);
    }

    /**
     * Sends an event with the given label and given integer data.
     *
     * @param key Key name must be in the form 'category_action'.
     * @param label An optional label. Can be null.
     * @param count The integer data associated with the event.
     */
    public static void event(Context context, String key, String label, long count) {
        if (sEnabled_NotForTesting) {
            EasyTracker et = EasyTracker.getInstance(context);
            if (et == null) { Log.w(TAG, "[event] GA Tracker null"); return; }

            key = key.replace('$', '-');

            int pos = key.indexOf('_');
            String category = pos == -1 ? key : key.substring(0, pos);
            String action = key;
            if (label == null) {
                label = action;
            } else {
                label = action + '_' + label;
            }

            et.send(MapBuilder.createEvent(category, action, label, count).build());
        }
    }
}
